package com.example.guava;

import com.google.common.collect.HashMultimap;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.google.common.collect.Sets;

import java.util.*;
import java.util.concurrent.ConcurrentMap;
import java.util.concurrent.CopyOnWriteArrayList;

/**
 * @author novel
 * @date 2020/10/30
 */
public class NewArrayList {
    public static void main(String[] args) {
        classifyGuava();
    }


    private static void classifyGuava() {
        // use guava
        HashMultimap<String, String> multimap = HashMultimap.create();
        multimap.put("狗", "大黄");
        multimap.put("狗", "旺财");
        multimap.put("猫", "加菲");
        multimap.put("猫", "汤姆");
        System.out.println(multimap.get("猫")); // [加菲, 汤姆]
    }

    private static void classifyJdk() {
//        HashMap<String, Set<String>> animalMap = new HashMap<>();
//        HashSet<String> dogSet = new HashSet<>();
//        dogSet.add("旺财");
//        dogSet.add("大黄");
//        animalMap.put("狗", dogSet);
//        HashSet<String> catSet = new HashSet<>();
//        catSet.add("加菲");
//        catSet.add("汤姆");
//        animalMap.put("猫", catSet);
//        System.out.println(animalMap.get("猫")); // [加菲, 汤姆]
    }

    private static void countGuava() {
//        ArrayList<String> arrayList = Lists.newArrayList("a", "b", "c", "d", "a", "c");
//        HashMultiset<String> multiset = HashMultiset.create(arrayList);
//        multiset.elementSet().forEach(s -> System.out.println(s + ":" + multiset.count(s)));
//        /*
//         * result:
//         * a:2
//         * b:1
//         * c:2
//         * d:1
//         */
    }

    private static void countJdk() {
//        // Java 统计相同元素出现的次数。
//        List<String> words = Lists.newArrayList("a", "b", "c", "d", "a", "c");
//        Map<String, Integer> countMap = new HashMap<String, Integer>();
//        for (String word : words) {
//            Integer count = countMap.get(word);
//            count = (count == null) ? 1 : ++count;
//            countMap.put(word, count);
//        }
//        countMap.forEach((k, v) -> System.out.println(k + ":" + v));
//        /*
//         * result:
//         * a:2
//         * b:1
//         * c:2
//         * d:1
//         */
    }

    private static void test2() {
        Set<String> newHashSet1 = Sets.newHashSet("a", "a", "b", "c");
        Set<String> newHashSet2 = Sets.newHashSet("b", "b", "c", "d");

        // 交集
        Sets.SetView<String> intersectionSet = Sets.intersection(newHashSet1, newHashSet2);
        System.out.println(intersectionSet); // [b, c]

        // 并集
        Sets.SetView<String> unionSet = Sets.union(newHashSet1, newHashSet2);
        System.out.println(unionSet); // [a, b, c, d]

        // newHashSet1 中存在，newHashSet2 中不存在
        Sets.SetView<String> setView = Sets.difference(newHashSet1, newHashSet2);
        System.out.println(setView); // [a]
    }

    private static void test1() {
        // 创建一个 ArrayList 集合
        List<String> list1 = Lists.newArrayList();
        // 创建一个 ArrayList 集合，同时塞入3个数据
        List<String> list2 = Lists.newArrayList("a", "b", "c");
        // 创建一个 ArrayList 集合，容量初始化为10
        List<String> list3 = Lists.newArrayListWithCapacity(10);

        LinkedList<String> linkedList1 = Lists.newLinkedList();
        CopyOnWriteArrayList<String> cowArrayList = Lists.newCopyOnWriteArrayList();

        HashMap<Object, Object> hashMap = Maps.newHashMap();
        ConcurrentMap<Object, Object> concurrentMap = Maps.newConcurrentMap();
        TreeMap<Comparable, Object> treeMap = Maps.newTreeMap();

        HashSet<Object> hashSet = Sets.newHashSet();
        HashSet<String> newHashSet = Sets.newHashSet("a", "a", "b", "c");
    }
}
